package com.luoye.lj.util;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Objects;

/**
 * 日期工具类
 *
 * @author Administrator
 */
public class DateUtil {

    /**
     * 日期对象转字符串
     *
     * @param date
     * @param format
     * @return
     */
    public static String formatDate(Date date, String format) {
        String result = "";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        if (date != null) {
            result = sdf.format(date);
        }
        return result;
    }

    /**
     * 字符串转日期对象
     *
     * @param str
     * @param format
     * @return
     * @throws Exception
     */
    public static Date formatString(String str, String format) {
        try {
            if (StringUtil.isEmpty(str)) {
                return null;
            }
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(str);
        }catch (Exception e){
            return  null;
        }
    }

    public static String getCurrentDateStr() throws Exception {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.format(date);
    }


    /**
     * 获取当前时间LocalDateTime
     *
     * @return LocalDateTime
     */
    public synchronized static LocalDateTime getNow() {
        return LocalDateTime.now();
    }

    /**
     * 将LocalDateTime转为自定义的时间格式的字符串
     *
     * @param localDateTime localDateTime
     * @param format        格式
     * @return 格式化时间
     */
    public synchronized static String getDateTimeAsString(LocalDateTime localDateTime, String format) {
        try {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
            return localDateTime.format(formatter);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 将long类型的timestamp转为LocalDateTime
     *
     * @param timestamp 时间戳
     * @return LocalDateTime
     */
    public synchronized static LocalDateTime getDateTimeOfTimestamp(long timestamp) {
        try {
            Instant instant = Instant.ofEpochMilli(timestamp);
            ZoneId zone = ZoneId.systemDefault();
            return LocalDateTime.ofInstant(instant, zone);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 将LocalDateTime转为long类型的timestamp
     *
     * @param localDateTime localDateTime
     * @return timestamp
     */
    public synchronized static long getTimestampOfDateTime(LocalDateTime localDateTime) {
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return instant.toEpochMilli();
    }

    /**
     * 将某时间字符串转为自定义时间格式的LocalDateTime
     *
     * @param time   时间
     * @param format 格式
     * @return LocalDateTime
     */
    public synchronized static LocalDateTime parseStringToDateTime(String time, String format) {
        try {
            DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
            return LocalDateTime.parse(time, df);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 将long类型的timestamp转为yyyy-mm-dd hh:ss:mm时间格式的字符串
     *
     * @param timestamp 时间戳
     * @return LocalDateTime
     */
    public synchronized static String parseTimestampToString(Long timestamp) {
        try {
            Instant instant = Instant.ofEpochMilli(timestamp);
            ZoneId zone = ZoneId.systemDefault();
            LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
            return getDateTimeAsString(localDateTime, "yyyy-MM-dd HH:mm:ss");
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * yyyy-mm-dd hh:ss:mm时间格式的字符串转时间戳
     *
     * @param time 时间
     * @return 时间戳
     */
    public synchronized static long parseStringToTimestamp(String time) {
        try {
            LocalDateTime localDateTime1 = parseStringToDateTime(time, "yyyy-MM-dd HH:mm:ss");
            return getTimestampOfDateTime(Objects.requireNonNull(localDateTime1));
        } catch (Exception e) {
            return 0;
        }
    }
}
